package graphisme.Checkers;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

// class reacting when a pawn is selected
public class ListenerCase implements MouseListener, Runnable {

	private Case case1;
	private CheckersGraphicalInterface plateau;
	JFrame frame = new JFrame("Information");

	public ListenerCase(Case case1, CheckersGraphicalInterface plateau) {
		super();
		this.case1 = case1;
		this.plateau = plateau;
	}

    // methodes inherit interface
	public void mouseClicked(MouseEvent arg0) {

	}

	public void mouseEntered(MouseEvent arg0) {

	}

	public void mouseExited(MouseEvent arg0) {

	}
	
// when click on a pawn, check the box 
	public void mousePressed(MouseEvent arg0) {
		if (case1.isSelectionnee()) {
			// get pawn's initial location
			int indiceLigne = plateau.getLigne(case1);
			int indiceColonne = plateau.getColonne(case1);
			System.out.println("la case active a : "
					+ plateau.getLigne(plateau.getCaseActive()) + "-"
					+ plateau.getColonne(plateau.getCaseActive()));
			System.out.println("o joue");
			plateau.play(case1);

			System.out.println("on set a false");
			// player has played -> his opponent's turn
			plateau.setYourTurn(false);
			// get pawn's final location
			int indiceLigneCaseActive = plateau.getLigne(plateau
					.getCaseTmpActive());
			int indiceColonneCaseActive = plateau.getColonne(plateau
					.getCaseTmpActive());
			System.out.println("on envoi : " + "caseMove_" + indiceLigne + "_"
					+ indiceColonne + "_" + indiceLigneCaseActive + "_"
					+ indiceColonneCaseActive);
			// send indication to opponent
			plateau.sendMsg("caseMove_" + indiceLigne + "_" + indiceColonne
					+ "_" + indiceLigneCaseActive + "_"
					+ indiceColonneCaseActive);
			// reset
			plateau.setCaseTmpActive(null);
			// check number of remaining pawn
			plateau.calculNbPionleft();
			// if there is at least one pawn left, the game continues
			if (plateau.getNbBlanc() != 0 && plateau.getNbNoir() != 0) {
				plateau.createMSgBOX("Waiting for player's move");
			}// else if there is not longer white pawn 
			else if (plateau.getNbBlanc() == 0) {
				System.out.println(" number of white pawn : O");
				plateau.clearFrame();
				plateau.sendMsg("Blanc Loose");
			}// else if there is not longer black pawn 
			else if (plateau.getNbNoir() == 0) {
				System.out.println(" number of black pawn : 0");
				plateau.clearFrame();
				plateau.sendMsg("Noir Loose");
			}

		}
	}

	public void mouseReleased(MouseEvent arg0) {

	}

	// inhereited method ->useless
	@Override
	public void run() {
		// TODO Auto-generated method stub

	}

}
